#include "building.h"

Building::Building(std::vector<Floor>& _f/*, double _k*/) : System(_f.size() - 1), floors(_f)/*, goundK(_k)*/{
}

Building::~Building(){
}

Floor Building::getFloor(unsigned int _index) const{
	return floors.at(_index);
}

unsigned int Building::sizeFloors() const {
	return floors.size();
}

void Building::draw() const {
	drawGround();
	
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	glColor3f(1.0, 1.0, 1.0);
	unsigned int sizeF = sizeFloors();
	for (unsigned int i = 0; i < sizeF; i++){
		drawFloor(i);
	}
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	
	for (unsigned int i = 0; i < sizeF; i++){
		glColor3f(getFloor(i).getColor().r,getFloor(i).getColor().g,getFloor(i).getColor().b);
		drawFloor(i);
	}

}

void Building::drawGround() const{
	//desenhando chão
	glColor3f(1.0, 1.0, 1.0);
	Vector3D ground(getFloor(0).getPosition());
	double w = getFloor(0).getWidth();
	double h = getFloor(0).getHeight();
	
	glBegin(GL_LINES);
	glVertex3f(ground.x - w, ground.y - (h / 2.0), ground.z);
	glVertex3f(ground.x + w, ground.y - (h / 2.0), ground.z);
	
	for (double i = - w; i <= w; i = i + 0.5){
		glVertex3f(ground.x + i , ground.y - (h / 2.0), ground.z);
		glVertex3f(ground.x + i - 0.2, ground.y - (h / 2.0) - 0.15, ground.z);
	}
	glEnd();
}

void Building::drawFloor(unsigned int _i) const{
	Floor f = getFloor(_i);
	Vector3D position(f.getPosition() + f.getX());
	///////////////////////////////***///////////////////////////////
	double pw = f.getWidth() / 2.0;
	double ph = f.getHeight() / 2.0;
	///////////////////////////////***///////////////////////////////
	
	glBegin(GL_QUADS);
	glVertex3f(position.x + pw, position.y + ph, position.z);
	glVertex3f(position.x - pw, position.y + ph, position.z);
	glVertex3f(position.x - pw, position.y - ph, position.z);
	glVertex3f(position.x + pw, position.y - ph, position.z);
	glEnd();
}

void Building::calculate(unsigned int t){
	unsigned int sizeF = sizeFloors() - 1;
	for (unsigned int i = 0; i < sizeF; i++){
		double dx = 0.0;
		for (unsigned int j = 0; j < sizeF; j++){
			dx += alphas.at(j) * getU(i, j) * sin((getW(j) * t) + gammas.at(j));
		}
		Vector3D d(dx, 0.0, 0.0);
		floors.at(i + 1).setX(d);
	}
}

void Building::initialPosition(){
	unsigned int sizeF = sizeFloors();
	for (unsigned int i = 1; i < sizeF; i++){
		getFloor(i).setX(Vector3D());
	}
}

void Building::populateK(Matrix& k){
	unsigned int sizeF = sizeFloors();
	for (unsigned int i = 1; i < sizeF; i++){
		for (unsigned int j = 1; j < sizeF; j++){
			if (i == j){
				if (i == sizeF - 1) k[i-1][j-1] = getFloor(i).getK();
				else k[i-1][j-1] = (getFloor(i).getK()) + (getFloor(i + 1).getK());
			}
			else if (i - j == 1)
				k[i-1][j-1] = -getFloor(i).getK();
			else if (j - i == 1)
				k[i-1][j-1] = -getFloor(i + 1).getK();
			else
				k[i-1][j-1] = 0.0;
		}
	}
	k.print();
}
/*
void Building::populateK(Matrix& k){
	unsigned int sizeF = sizeFloors();
	for (unsigned int i = 1; i < sizeF; i++){
		for (unsigned int j = 1; j < sizeF; j++){
			if (i == j){
				if (i == sizeF - 1) k[i-1][j-1] = getFloor(i).getK();
				else k[i-1][j-1] = getFloor(i).getK() + getFloor(i + 1).getK();
			}
			else if (i - j == 1)
				k[i-1][j-1] = -getFloor(i).getK();
			else if (j - i == 1)
				k[i-1][j-1] = -getFloor(i + 1).getK();
			else
				k[i-1][j-1] = 0.0;
		}
	}
}*/

void Building::populateM(Matrix& m){	//area
	unsigned int sizeF = sizeFloors();
	for(unsigned int i = 1; i < sizeF; i++){
		m[i-1][i-1] = getFloor(i).getWidth() * getFloor(i).getHeight();
	}
}
